<!--
 * @Author: ljc
 * @Date: 2023-04-25 15:00:24
 * @LastEditors: g05047
 * @LastEditTime: 2023-05-11 09:20:00
 * @Description: 备选项目申报添加项目弹框
-->
<template>
  <div>
    <rk-dialog width="1200"
      height="500"
      :title="title"
      :visible="visible"
      :destroyOnClose="false"
      @close="visible = false"
      :maskClosable="false">
      <rk-container :isFooter="false"
        :isAside="false"
        :isHeader='false'>
        <template #main>
          <rk-condition>
            <template #condition>
              <rk-condition-bar>
                <template #content>
                  <el-form ref="form"
                    :inline="true"
                    label-width="80px">
                    <el-form-item label="项目性质">
                      <el-input class="mr-15px w-200px"
                        v-model="form.name1"></el-input>
                    </el-form-item>
                    <el-form-item label="项目类型">
                      <el-input class="mr-15px w-200px"
                        v-model="form.name2"></el-input>
                    </el-form-item>
                    <el-form-item label="建设状态">
                      <el-input class="mr-15px w-200px"
                        v-model="form.name3"></el-input>
                    </el-form-item>
                    <el-form-item label="模糊查询">
                      <el-input class="mr-15px w-200px"
                        v-model="form.name"></el-input>
                    </el-form-item>
                  </el-form>
                </template>
                <template #buttons>
                  <el-button type="primary">查询</el-button>
                  <el-button>重置</el-button>
                </template>
              </rk-condition-bar>
            </template>
            <template #content>
              <rk-grid ref="alternateProjectRef"
                dataUrl="/alternateProject/alternateProjectPage"
                :params="queryParams"
                :columns="tableColumns"
                :boxConfig="{ mode: 'radio' , fixed: true }">
                <template #operation="{ row }">
                  <div class="operation-box">
                    <el-button type="primary"
                      link>设计</el-button>
                    <el-button type="primary"
                      link>预览</el-button>
                  </div>
                </template>
                <template #status="{ row }">
                  <el-tag :type="COMMON_STOP_OPEN_ENUMS.options[row.status].tagType"
                    class="mx-1"
                    effect="plain">
                    {{ COMMON_STOP_OPEN_ENUMS.options[row.status].echo }}
                  </el-tag>
                </template>
              </rk-grid>
            </template>
          </rk-condition>
        </template>
      </rk-container>
      <template #footer>
        <el-button type="primary"
          @click="handleConfirm">保存</el-button>
        <el-button @click="hanldeClose">取消</el-button>
      </template>
    </rk-dialog>
    <ReserveProjDialog ref="standbyProjDialogRef"
      type="standby"
      :formId="standbyFormId"
      :processKey="processKey" />
  </div>
</template>

<script setup>
import { useNamespace } from "element-plus";
import { COMMON_STOP_OPEN_ENUMS } from '@/enums/enums-common.js'
import { ElMessage } from 'element-plus'
import { reactive, ref } from "vue"
import ReserveProjDialog from '@/components/form/business/ReserveProjDialog.vue'
import XEUtils from 'xe-utils'

const ns = useNamespace("page", ref("rk"));
const emit = defineEmits(['refresh'])
const alternateProjectRef = ref(null)
let form = reactive({
  name: undefined,
  name1: undefined,
  name2: undefined,
  name3: undefined,
})
let selectData = reactive({})
const title = ref('添加项目')
const visible = ref(false)
const processKey = ref(null)
// 查询参数
let queryParams = reactive({
  proStatusCode: undefined,
  status: undefined,
  mhcx: undefined,
  adCode: undefined,
  agCode: undefined,
  processKey: undefined,
  proType: undefined
})
const standbyProjDialogRef = ref(null)
// 储备项目ID
const selectProId = ref('')
// 备选项目的表单ID
const standbyFormId = '5FD8F682CD8446DEA55D4AD3DA7D3909'//死
// 基本信息
const baseInfo = reactive({
  // 选中项目ID
  proId: undefined,
  // 选中区划编码
  adCode: undefined,
  // 单位ID
  agId: undefined,
  // 选中单位编码
  agCode: undefined,
  // 单位名称
  agName: undefined,
  // 项目来源：1：基础库 2：储备库 3:需求库 4：历史项目 5、一体化同步项目 6、存量债务无项目生成项目
  proSource: undefined,
})
const tableColumns = ref([
  { field: 'agencyName', title: '申报单位', width: 250, align: 'left', },
  {
    field: 'proName', title: '项目名称', width: 250, align: 'left',
    renderer({ cellValue, row, column }) {
      return '<a  rel="opener" href="/page/debt/project-manage/common/pro-yhs.html?pro_id=' + row.PRO_ID + '" target="_blank">' + cellValue + '</a>';
    }
  },
  { field: 'proCode', title: '项目编码', width: 200, align: 'left', },
  { field: 'fiscalYear', title: '年度', width: 120, align: 'center', },
  { field: 'debtDeadlineName', title: '债券期限', width: 150, align: 'center', },
  {
    field: 'applyAmt',
    title: '申请金额（万元）',
    formatter: 'formatThousand',
    width: 200,
    sumable: true,
    align: 'right'
  },
  {
    field: 'proCapital',
    title: '项目资本金（万元）',
    formatter: 'formatThousand',
    width: 200,
    sumable: true,
    align: 'right'
  },
  {
    field: 'bondTypeName', title: '申请类型', width: 150, align: 'left'
  },
  { field: 'applyDate', title: '申报日期', width: 200, align: 'center' },
  { field: 'setupYear', title: '立项年度', width: 180, align: 'center' },
  { field: 'xmxzName', title: '项目性质', width: 150, align: 'left' },
  { field: 'proTypeName', title: '项目类型', width: 150, align: 'left' },
  { field: 'fundInvestAreaName', title: '资金投向领域', width: 250, align: 'left' },
  { field: 'startDate', title: '开工日期', width: 150, align: 'center' },
  { field: 'endDate', title: '竣工日期', width: 150, align: 'center' },
  { field: 'buildStatusName', title: '建设状态', width: 150, align: 'left' },
])

/** 打开弹出层 */
const handleOpen = (params, model) => {
  visible.value = true
  queryParams.adCode = params.adCode
  queryParams.agCode = params.agCode
  queryParams.processKey = params.processKey
  processKey.value = params.processKey
  selectData = {
    ...model
  }
}
/** 关闭弹窗 */
const hanldeClose = () => {
  visible.value = false
}

/** 确认提交 */
const handleConfirm = () => {
  const seleRow = alternateProjectRef.value.getRadioRecord()
  selectProId.value = seleRow.proId
  baseInfo.proId = seleRow.proId
  baseInfo.adCode = queryParams.adCode
  // baseInfo.agId = seleRow.agId
  baseInfo.agCode = queryParams.agCode
  baseInfo.agName = seleRow.agencyName
  if (!XEUtils.isEmpty(selectData)) {
    handleEdit()
  } else {
    handleAdd()
  }
  emit('refresh')
  visible.value = false
}

const handleAdd = () => {
  const addInfo = toRaw(baseInfo)
  addInfo.proId = selectProId.value
  addInfo.proType = 'sp'
  standbyProjDialogRef.value.handleAdd(addInfo)
}


// 对外暴露方法
defineExpose({
  handleOpen
})
</script>

<style lang="scss" scoped>
</style>